package com.kzd.btgame.main.money

import android.view.LayoutInflater
import androidx.lifecycle.LifecycleOwner
import com.kzd.btgame.R
import com.kzd.btgame.custom.setTransitionStatus
import com.kzd.btgame.databinding.ActivityMoneyTaskBinding
import com.kzd.btgame.jetpack.vm.MoneyViewModel
import com.kzd.btgame.main.KEY_MONEY_TASK_TYPE
import com.kzd.btgame.main.money.adapter.MoneyTaskListAdapter
import com.kzd.btgame.main.popup.SystemTipsPopup
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel

class MoneyTaskActivity : OwnerViewBindingActivity<ActivityMoneyTaskBinding>() {

    private val mSystemTipsPopup: SystemTipsPopup by lazy {
        SystemTipsPopup(context = this)
    }

    private val mMoneyTaskListAdapter = MoneyTaskListAdapter()

    private val mType: String by lazy {
        intent?.extras?.getString(KEY_MONEY_TASK_TYPE) ?: ""
    }

    private val mViewModel:MoneyViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityMoneyTaskBinding
        get() = ActivityMoneyTaskBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.tlMoneyTaskBar
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {
        mViewBinding.rvMoneyTaskBody.adapter = mMoneyTaskListAdapter

        mMoneyTaskListAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData = mMoneyTaskListAdapter.getItem(position)
            if (view.id != R.id.tvMoneyTaskListGo) {
                return@setOnItemChildClickListener
            }
            if (itemData.verify_rs == 1) {
                mViewModel.moneyTaskDayGet(itemData.type)
            } else if (itemData.verify_rs == 0 && itemData.type == "today_login") {
                mSystemTipsPopup.setMessage("每日登录游戏就可领取")
                    .showPopupWindow()
            } else if (itemData.verify_rs == 0 && itemData.type == "today_recharge") {
                mSystemTipsPopup.setMessage("仅计算现金/平台币充值游戏")
                    .showPopupWindow()
            } else if (itemData.verify_rs == 0 && itemData.type == "today_share") {
                mViewModel.userShare(
                    activity = this, url = itemData.url,
                    title = "${getString(R.string.app_name)}--领1000元",
                    desc = "注册即享千元福利，千款手游特权"
                )
            } else if (itemData.verify_rs == 0 && itemData.type == "today_pinglun") {
                mSystemTipsPopup.setMessage("去评论，评论成功即可获取奖励")
                    .showPopupWindow()
            }
        }
    }

    override fun initData() {
        if (mType == "newbieTask") {
            mViewModel.moneyTaskNew()
        } else if (mType == "todayTask") {
            mViewModel.moneyTaskDay()
        }
    }

    override fun initViewMode() {
        mViewModel.moneyTaskLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mMoneyTaskListAdapter.setNewInstance(data)
        })

        mViewModel.moneyTaskDayGetLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            initData()
        })

        mViewModel.userIsShareLiveData.observe(this as LifecycleOwner, {
            mViewModel.moneyTaskAddShare()
        })
    }
}